//校验邮箱格式
function checkEmail(email){
    return RegExp(/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/).test(email);
}
//校验手机格式
function checkMobile(mobile){
    return RegExp(/^1[345789]\d{9}$/).test(mobile);
}
//校验身份证号
function validateIDCard(idCard) {
  const regex = /^[1-9]\d{5}(18|19|20|21|22)?\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}(\d|[Xx])$/;
  return regex.test(idCard);
}

//将富文本转成普通文本
function removeHtmlStyle(html){
  const relStyle = /style\s*?=\s*?(['"])[\s\S]*?\1/g
  const relTag = /<.+?>/g
  const relClass = /class\s*?=\s*?(['"])[\s\S]*?\1/g
  let newHtml = ''
  if (html) {
    newHtml = html.replace(relStyle, '')
    newHtml = newHtml.replace(relTag, '')
    newHtml = newHtml.replace(relClass, '')
  }
  return newHtml
}
//把传入的完整时间分为 MM月DD日 的格式
function formatDate(timeStr) {
	const date = new Date(timeStr);
	const month = (date.getMonth() + 1).toString().padStart(2, "0"); // 获取月份，并补齐为两位
	const day = date.getDate().toString().padStart(2, "0"); // 获取日期，并补齐为两位
	return `${month}.${day}`;
}
//时间戳转时间
function timestampToTime(timestamp) {
  var date = new Date(timestamp); // 时间戳为毫秒级别
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var day = date.getDate();
  var hours = date.getHours();
  var minutes = date.getMinutes();
  var seconds = date.getSeconds();
 
  // 补零操作
  month = month < 10 ? '0' + month : month;
  day = day < 10 ? '0' + day : day;
  hours = hours < 10 ? '0' + hours : hours;
  minutes = minutes < 10 ? '0' + minutes : minutes;
  seconds = seconds < 10 ? '0' + seconds : seconds;
 
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

//时间戳转时间-准确到分
function timestampToTime2(timestamp) {
  var date = new Date(timestamp); // 时间戳为毫秒级别
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var day = date.getDate();
  var hours = date.getHours();
  var minutes = date.getMinutes();
 
  // 补零操作
  month = month < 10 ? '0' + month : month;
  day = day < 10 ? '0' + day : day;
  hours = hours < 10 ? '0' + hours : hours;
  minutes = minutes < 10 ? '0' + minutes : minutes;
 
  return `${year}-${month}-${day} ${hours}:${minutes}`;
}

//时间戳转日期
function timestampToDate(timestamp) {
  var date = new Date(timestamp); // 时间戳为毫秒级别
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var day = date.getDate();
  var hours = date.getHours();
  var minutes = date.getMinutes();
  var seconds = date.getSeconds();
 
  // 补零操作
  month = month < 10 ? '0' + month : month;
  day = day < 10 ? '0' + day : day;
 
  return `${year}-${month}-${day}`;
}

//时间戳转年月
function timestampToMonth(timestamp) {
  var date = new Date(timestamp); // 时间戳为毫秒级别
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
 
  // 补零操作
  month = month < 10 ? '0' + month : month;
 
  return `${year}-${month}`;
}

module.exports = {
    checkEmail : checkEmail,
    checkMobile : checkMobile,
	validateIDCard: validateIDCard,
	removeHtmlStyle: removeHtmlStyle,
	formatDate: formatDate,
	timestampToTime: timestampToTime,
	timestampToTime2: timestampToTime2,
	timestampToDate: timestampToDate,
	timestampToMonth: timestampToMonth,
}